package per.ethan.springboot.basic.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import per.ethan.springboot.basic.domain.ResultBean;
import per.ethan.springboot.basic.entity.BaseEntity;
import per.ethan.springboot.basic.entity.Page;
import per.ethan.springboot.basic.service.BaseService;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Ethan
 * @date 2020/06/18 14:58
 */
@SuppressWarnings("all")
public abstract class BaseController<Entity extends BaseEntity, Service extends BaseService<Entity>> {

    @Autowired
    private Service service;

    @PostMapping
    @ApiOperation(value = "新增实体信息")
    public ResultBean<Long> save(@Valid @RequestBody Entity entity) {
        return ResultBean.success(service.create(entity));
    }

    @PostMapping(value = "/batchSave")
    @ApiOperation(value = "批量新增实体信息")
    public ResultBean<List<Long>> batchSave(@RequestBody List<Entity> entityList) {
        return ResultBean.success(service.batchSave(entityList));
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据ID删除实体信息")
    public ResultBean<Boolean> deleteById(@PathVariable(value = "id") Long id) {
        return ResultBean.success(service.removeById(id));
    }

    @DeleteMapping
    @ApiOperation(value = "根据ID批量删除实体信息")
    public ResultBean<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return ResultBean.success(service.removeByIds(ids));
    }

    @DeleteMapping(value = "/logic/{id}")
    @ApiOperation(value = "根据ID逻辑删除实体信息")
    public ResultBean<Boolean> logicDeleteById(@PathVariable(value = "id") Long id) {
        return ResultBean.success(service.logicRemoveById(id));
    }

    @DeleteMapping(value = "/logic")
    @ApiOperation(value = "根据ID批量逻辑删除实体信息")
    public ResultBean<Boolean> logicBatchDelete(@RequestBody List<Long> ids) {
        return ResultBean.success(service.logicRemoveByIds(ids));
    }

    @DeleteMapping(value = "/deleteByWhere")
    @ApiOperation(value = "根据特殊条件删除实体信息")
    public ResultBean<Boolean> logicBatchDelete(@RequestBody Entity entity) {
        return ResultBean.success(service.removeByWhere(entity));
    }

    @DeleteMapping(value = "/logicDeleteByWhere")
    @ApiOperation(value = "根据特殊条件逻辑删除实体信息")
    public ResultBean<Boolean> logicDeleteByWhere(@RequestBody Entity entity) {
        return ResultBean.success(service.logicRemoveByWhere(entity));
    }

    @PutMapping
    @ApiOperation(value = "修改实体信息")
    public ResultBean<Boolean> update(@RequestBody Entity entity) {
        return ResultBean.success(service.updateById(entity));
    }

    @PutMapping(value = "/batchUpdate")
    @ApiOperation(value = "批量修改实体信息")
    public ResultBean<Boolean> batchUpdate(@RequestBody List<Entity> entityList) {
        return ResultBean.success(service.updateBatchById(entityList));
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询所有实体信息")
    public ResultBean<List<Entity>> list() {
        return ResultBean.success(service.list());
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据ID查询实体信息")
    public ResultBean<Entity> getById(@PathVariable(value = "id") Long id) {
        return ResultBean.success(service.getById(id));
    }

    @GetMapping(value = "/listByWhere")
    @ApiOperation(value = "根据特定条件查询")
    public ResultBean<List<Entity>> getByWhere(Entity entity) {
        return ResultBean.success(service.listByWhere(entity));
    }

    @PostMapping(value = "/listByPage")
    @ApiOperation(value = "分页查询")
    public ResultBean<Page<Entity>> getByPage(@RequestBody Page<Entity> page) {
        return ResultBean.success(service.listByPage(page));
    }

}
